package com.asistencia.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.asistencia.dao.interfaces.CourseDao;
import com.asistencia.domain.Course;
import com.asistencia.domain.TypeCourse;


@Repository
@SuppressWarnings({ "rawtypes", "unchecked" })
public class CourseJdbc implements CourseDao{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<Course> listAll() throws DataAccessException {
		String SQL="SELECT * FROM course";
		List matches=jdbcTemplate.query(SQL, new RowMapper(){
			
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
				Course course= new Course();
				TypeCourse type = new TypeCourse();
			
				course.setIdCourse(rs.getInt("idcourse"));
				course.setName(rs.getString("name"));
				type.setIdTypeCourse(rs.getInt("idcourse"));
				course.setMyTypeCourse(type);
				return course;
				
			}
			});
			
			return (matches.size() > 0 ? (List<Course>) matches : null);
	}	

	@Override
	public List<Course> listByName(String name) throws DataAccessException {
				String SQL="SELECT * FROM course WHERE name LIKE ? ";
			List matches=jdbcTemplate.query(SQL, new Object []{"%"+name+"%", "%" +name+"%"}, new RowMapper(){
			@Override
				public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
					Course course=new Course();
					TypeCourse type = new TypeCourse();
					
					course.setIdCourse(rs.getInt("idcourse"));
					course.setName(rs.getString("name"));
					type.setIdTypeCourse(rs.getInt("idcourse"));
					course.setMyTypeCourse(type);
					return course;
					
				}});
		return (matches.size()>0 ? (List<Course>)matches: null);
	}


	@Override
	public List<Course> listByTypeCourse(Integer typeCourse)
			throws DataAccessException {
		String SQL="SELECT * FROM course WHERE idtype_course = ?";
		List matches= jdbcTemplate.query(SQL, new Object []{typeCourse}, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
				Course course=new Course();
				TypeCourse type = new TypeCourse();
				
				course.setIdCourse(rs.getInt("idcourse"));
				course.setName(rs.getString("name"));
				type.setIdTypeCourse(rs.getInt("idcourse"));
				course.setMyTypeCourse(type);
				return course;
				
			}});
	return (matches.size()>0 ? (List<Course>)matches: null);
	}

	
	@Override
	public Course getById(Integer idCourse) throws DataAccessException {
		String SQL= "SELECT * FROM course WHERE idcourse = ?";
		List matches=jdbcTemplate.query(SQL, new Object[]{idCourse}, new RowMapper(){
			
		@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
				Course course=new Course();
				TypeCourse type = new TypeCourse();
				
				course.setIdCourse(rs.getInt("idcourse"));
				course.setName(rs.getString("name"));
				type.setIdTypeCourse(rs.getInt("idtype_course"));
				course.setMyTypeCourse(type);
				return course;
				
			}});
		
		return (matches.size() > 0 ? (Course) matches.get(0) : null);
		}
	}